package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.mindmap.DefaultMindmapNode;
import org.primefaces.model.mindmap.MindmapNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.engine.SpringUtil;
import co2.pojos.MindMenu;

@ManagedBean
@ViewScoped
public class MindmapBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

private MindmapNode root;
    
    private MindmapNode selectedNode;
    
    @Autowired
    @Qualifier("daoI")
    private DAOInterface dao;
    
    @PostConstruct
    public void postCon() {
    	
    	MindMenu forRoot =(MindMenu) dao.get(MindMenu.class, "123");
    	
        root = new DefaultMindmapNode(forRoot.getLabel(), forRoot.getData(), forRoot.getFill(), false);
        
        List<MindMenu> list = dao.getMindMenuByParent("123");
        MindMenu fc = null;
        MindmapNode child = null;
        
        if(list != null && list.size()>0){
        	for(int a=0;a<list.size();a++){
        		fc = list.get(a);
        		
        		child = new DefaultMindmapNode(fc.getLabel(),fc.getData(),fc.getFill(),true);
        		root.addNode(child);
        	}
        }
        MindmapNode ips = new DefaultMindmapNode("IPs", "IP Numbers", "6e9ebf", true);
        MindmapNode ns = new DefaultMindmapNode("NS(s)", "Namespaces", "6e9ebf", true);
        MindmapNode malware = new DefaultMindmapNode("Malware", "Malicious Software", "6e9ebf", true);
        
        
        root.addNode(ips);
        root.addNode(ns);
        root.addNode(malware);
    }

    public MindmapNode getRoot() {
        return root;
    }

    public MindmapNode getSelectedNode() {
        return selectedNode;
    }
    public void setSelectedNode(MindmapNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public void onNodeSelect(SelectEvent event) {
        MindmapNode node = (MindmapNode) event.getObject();
        
        //populate if not already loaded
        if(node.getChildren().isEmpty()) {
            Object label = node.getLabel();

            if(label.equals("NS(s)")) {
                for(int i = 0; i < 25; i++) {
                    node.addNode(new DefaultMindmapNode("ns" + i + ".google.com", "Namespace " + i + " of Google", "82c542"));
                }
            }
            else if(label.equals("IPs")) {
                for(int i = 0; i < 18; i++) {
                    node.addNode(new DefaultMindmapNode("1.1.1."  + i, "IP Number: 1.1.1." + i, "fce24f"));
                } 

            }
            else if(label.equals("Malware")) {
                for(int i = 0; i < 18; i++) {
                    String random = UUID.randomUUID().toString();
                    node.addNode(new DefaultMindmapNode("Malware-"  + random, "Malicious Software: " + random, "3399ff", false));
                }
            }
        }
        
    }
    
    public void onNodeDblselect(SelectEvent event) {
        this.selectedNode = (MindmapNode) event.getObject();        
    }

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
    
}
